package com.ruoyi.dataSys.controller;

import org.apache.poi.ss.usermodel.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/excel")
public class ExcelController {

    @PostMapping("/import")
    public List<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        List<List<String>> data = new ArrayList<>();

        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

        for (Row row : sheet) {
            List<String> rowData = new ArrayList<>();
            for (Cell cell : row) {
                rowData.add(cell.toString()); // 将单元格内容转换为字符串
            }
            data.add(rowData);
        }

        workbook.close();
        return data; // 返回解析后的数据
    }
}
